package com.linghu.qqclient.service;

import com.linghu.qqcommon.Message;
import com.linghu.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * @author 令狐荣豪
 * @version 1.0
 * 该类和对象，提供消息相关的服务方法
 */
public class MessageClientService {
    /**
     *
     * @param content 内容
     * @param senderID 发送者
     */
    public void sendMessageToAll(String content,String senderId) throws IOException {
        //构建message对象
        Message message=new Message();
        message.setMesType(MessageType.MESSAGE_TO_ALL_MES);//普通聊天消息
        message.setSender(senderId);
        message.setContent(content);
        message.setSendtime(new Date().toString());//发送时间设置到message对象
        System.out.println(senderId+"对大家说："+content);

        //发送给服务端
        ObjectOutputStream oos = new
                ObjectOutputStream(ManagerClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
        oos.writeObject(message);

    }
    /**
     *
     * @param content 发送内容
     * @param senderId 发送者id
     * @param getterId  接收用户的id
     */
    public void sendMessageToOne(String content,String senderId,String getterId) throws IOException {
        //构建message对象
        Message message=new Message();
        message.setMesType(MessageType.MESSAGE_COMM_MES);//普通聊天消息
        message.setSender(senderId);
        message.setGetter(getterId);
        message.setContent(content);
        message.setSendtime(new Date().toString());//发送时间设置到message对象
        System.out.println(senderId+"对"+getterId+"说"+content);

        //发送给服务端
        ObjectOutputStream oos = new
                ObjectOutputStream(ManagerClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
        oos.writeObject(message);
    }
}
